package cn.com.tianpeidong.restaurantadmin.project.admin.pos.rest;

import cn.com.tianpeidong.restaurantadmin.project.admin.pos.entity.Sale;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.entity.SaleLineItem;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.service.IPosService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author tianpeidong
 * @version 创建时间: 19-5-8 上午11:17
 */
@RestController
@RequestMapping("/pos")
public class PosController {

    @Autowired
    private IPosService posService;

    @PostMapping
    @RequiresPermissions("pos:add")
    public ResponseEntity newSale(@RequestBody Sale sale) {
        return ResponseEntity.ok(posService.newSale(sale));
    }

    @GetMapping
    @RequiresPermissions("pos:list")
    public ResponseEntity list(@RequestParam("deskId") Long deskId) {
        return ResponseEntity.ok(posService.getSale(deskId));
    }

    @PutMapping("/addItem")
    @RequiresPermissions("pos:add")
    public ResponseEntity addItem(@RequestParam("deskId") Long deskId, @RequestBody SaleLineItem saleLineItem) {
        return ResponseEntity.ok(posService.add(deskId, saleLineItem));
    }

    @DeleteMapping
    @RequiresPermissions("pos:delete")
    public ResponseEntity dropSale(@RequestParam("deskId") Long saleId) {
        posService.dropSale(saleId);
        return ResponseEntity.noContent().build();
    }

    @PutMapping("/deleteItem")
    @RequiresPermissions("pos:delete")
    public ResponseEntity deleteItem(@RequestParam("deskId") Long deskId, @RequestBody SaleLineItem saleLineItem) {

        return ResponseEntity.ok(posService.removeDishes(deskId, saleLineItem));
    }

    @GetMapping("/getTotal")
    @RequiresPermissions("pos:list")
    public ResponseEntity genTotal(@RequestParam("deskId") Long deskId) {
        return ResponseEntity.ok(posService.calPrice(deskId));
    }

    @PutMapping
    public ResponseEntity makeSale(@RequestParam("deskId") Long deskId) {
        return ResponseEntity.ok(posService.makeSale(deskId));
    }

}
